<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\tool\Result;
use think\facade\View;
use think\Response;
use addons\Builder\facade\Builder;

class Role extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Role';

    //验证器
    protected string $validate = 'Role';

    //自定义方法排序
    protected array $sort = ['id' => 'desc'];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '角色名', 'modify' => '2,input'])
            ->addColumn(['field' => 'explain', 'title' => '角色说明', 'modify' => '2,input'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#switchStatus', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 220])
            ->addRightButtons([
                'give' => [
                    'title' => '分配权限',
                    'url' => url('role/give')->build(),
                    'event' => '_openDrawer',
                    'size' => '60%',
                ],
                'edit' => ['size' => '50%'],
                'delete'
            ])
            ->addTopButtons(['add' => ['size' => '50%']])
            ->addSearch([
                ['title', '角色名', 'input,Like']
            ])
            ->addExtraHtml(['role'])
            ->fetch();
    }

    public function give($id = ''): Response|string
    {
        if ($this->request->isAjax()) {
            try {
                $param = $this->request->post();
                $param['rules'] = $param['rules'] ? implode(',', $param['rules']) : '';
                $this->model::update($param);
                return Result::success('操作成功');
            } catch (\Exception $e) {
                return Result::fail($e->getMessage());
            }
        }

        extract((new \app\common\model\Menu())->getAuth($id));

        function childrenFun($item, $rule)
        {
            $children = [];
            foreach ($item['child'] as $child) {
                $children[] = [
                    'id' => $child['id'],
                    'title' => $child['title'],
                    'spread' => true,
                    'children' => childrenFun($child, $rule),
                    'checked' => in_array($child['id'], $rule)
                ];
            }
            return $children;
        }

        $list = [];
        foreach ($menu as $item) {
            $data = [
                'id' => $item['id'],
                'title' => $item['title'],
                'spread' => true,
                'children' => childrenFun($item, $rule)
            ];
            $list[] = $data;
        }

        return View::assign('list', $list)->fetch();
    }
}